package com.qianji.petmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianji.common.constant.ProductConstant;
import com.qianji.common.utils.PageUtils;
import com.qianji.common.utils.Query;
import com.qianji.petmall.product.dao.AttrAttrgroupRelationDao;
import com.qianji.petmall.product.dao.AttrDao;
import com.qianji.petmall.product.dao.AttrGroupDao;
import com.qianji.petmall.product.dao.CategoryDao;
import com.qianji.petmall.product.entity.AttrAttrgroupRelationEntity;
import com.qianji.petmall.product.entity.AttrEntity;
import com.qianji.petmall.product.entity.AttrGroupEntity;
import com.qianji.petmall.product.entity.CategoryEntity;
import com.qianji.petmall.product.service.AttrService;
import com.qianji.petmall.product.service.CategoryService;
import com.qianji.petmall.product.vo.AttrRespVo;
import com.qianji.petmall.product.vo.AttrVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Autowired
    private AttrDao attrDao;

    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private AttrGroupDao attrGroupDao;

    @Autowired
    private CategoryService categoryService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveAttr(AttrVo attr) {
        AttrEntity entity = new AttrEntity();
        BeanUtils.copyProperties(attr,entity);
        baseMapper.insert(entity);
        if(attr.getAttrType() == ProductConstant.ArrEnum.TYPE_BASE.getCode() && attr.getAttrGroupId() != null) {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            relationEntity.setAttrId(entity.getAttrId());
            relationEntity.setAttrGroupId(attr.getAttrGroupId());
            attrAttrgroupRelationDao.insert(relationEntity);
        }
    }

    @Override
    public PageUtils queryAllList(Map<String, Object> params, Long catId, String type) {
        log.info("得到的信息是{},得到的id为{}",params,catId);
        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>().eq("attr_type","base".equals(type)?ProductConstant.ArrEnum.TYPE_BASE.getCode() : ProductConstant.ArrEnum.TYPE_SALE.getCode());
        String key = (String) params.get("key");
        if(StringUtils.isNotEmpty(key)){
            queryWrapper.eq("attr_id",key).or().like("attr_name",key);
        }

        if (catId != 0){
            queryWrapper.eq("catelog_id",catId);
        }

        IPage<AttrEntity> page = this.page(new Query<AttrEntity>().getPage(params), queryWrapper);
        PageUtils pageUtils = new PageUtils(page);
        List<AttrEntity> records = page.getRecords();
        // 查到属性后还要结合分类名字、分组名字(分类->属性->分组) 封装为AttrRespVo对象
        List<AttrRespVo> attrRespVos = records.stream().map((attrEntity) -> {
            AttrRespVo attrRespVo = new AttrRespVo();
            BeanUtils.copyProperties(attrEntity, attrRespVo);

            // 1.设置分类和分组的名字  先获取中间表对象  给attrRespVo 封装分组名字
                // 根据属性id查询关联表，得到其属性分组
            if(attrEntity.getAttrType() == ProductConstant.ArrEnum.TYPE_BASE.getCode()) {
                AttrAttrgroupRelationEntity entity = attrAttrgroupRelationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
                if (entity != null && entity.getAttrGroupId() != null) {
                    AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(entity.getAttrGroupId());
                    // 设置属性分组的名字
                    if (attrGroupEntity != null) {
                        attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
                    }
                }
            }

            // 2.查询分类id 给attrRespVo 封装三级分类名字
            CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
            if (categoryEntity != null) {
                attrRespVo.setCateLogName(categoryEntity.getName());
            }
            return attrRespVo;
        }).collect(Collectors.toList());
        pageUtils.setList(attrRespVos);
        return pageUtils;
    }

    @Override
    public AttrRespVo selectPath(Long attrId) {
        AttrRespVo attrRespVo = new AttrRespVo();
        //查询出当前用户的所有消息
        AttrEntity attr = baseMapper.selectById(attrId);
        BeanUtils.copyProperties(attr,attrRespVo);
        if(attr.getAttrType() == ProductConstant.ArrEnum.TYPE_BASE.getCode()) {
            //根据信息查询当前分组
            AttrAttrgroupRelationEntity id = attrAttrgroupRelationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId));
            if(id != null) {
                attrRespVo.setAttrGroupId(id.getAttrGroupId());
            }
            AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrRespVo.getAttrGroupId());
            if (attrGroupEntity != null) {
                attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
            }
        }


        //获得当前分类信息
        Long catelogId = attr.getCatelogId();
        Long[] atterPath = categoryService.getAtterPath(catelogId);
        attrRespVo.setCatelogPath(atterPath);
        CategoryEntity category = categoryDao.selectById(catelogId);
        if(category != null) {
            attrRespVo.setCateLogName(category.getName());
        }
        return attrRespVo;
    }

    @Override
    public void updateAttr(AttrVo attr) {
        log.info("得到的数据{}",attr);
        AttrEntity entity = new AttrEntity();
        BeanUtils.copyProperties(attr,entity);
        //先修改AttrEntity的数据
        baseMapper.updateById(entity);
        if(entity.getAttrType() == ProductConstant.ArrEnum.TYPE_BASE.getCode()) {
            //修改分组的数据
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            relationEntity.setAttrGroupId(attr.getAttrGroupId());
            relationEntity.setAttrId(attr.getAttrId());

            Integer id = attrAttrgroupRelationDao.selectCount(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attr.getAttrId()));
            if(id > 0){
                attrAttrgroupRelationDao.update(relationEntity,  new UpdateWrapper<AttrAttrgroupRelationEntity>().eq("attr_id",attr.getAttrId()));
            }else {
                attrAttrgroupRelationDao.insert(relationEntity);
            }
        }


    }

    @Override
    public List<Long> selectSearchAttrIds(List<Long> collect) {
        List<Long> longList = attrDao.selectSearchAttrIds(collect);
        return longList;
    }

    @Override
    public List<AttrEntity> getRelationAttr(Long list) {
        List<AttrAttrgroupRelationEntity> attrId = attrAttrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", list));
        List<Long> collect = attrId.stream().map((arr) -> {
            return arr.getAttrId();
        }).collect(Collectors.toList());
        if (collect == null || collect.size()==0) {
            return null;
        }
        List<AttrEntity> attrEntities = this.listByIds(collect);
        if (attrEntities == null) {
            return null;
        }
        return attrEntities;
    }


}